package com.iqiyi.pps.epg.core.dao.stream;

import com.iqiyi.pps.epg.core.dao.base.BaseDao;
import com.iqiyi.pps.epg.core.model.stream.LiveChannelStream;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/12/4.
 */
@Component
public class ChannelStreamBaseDao extends BaseDao<LiveChannelStream> {

    private static final String QUERY_CHANNELSTREAM_BY_ID = "select s from LiveChannelStream s where s.id=? and s.status<>-1";
    private static final String QUERY_CHANNELSTREAM_BY_STREAMNAME = "select s from LiveChannelStream s where s.streamName=? and s.status<>-1";
    private static final String QUERY_CHANNELSTREAM_BY_DOLBYSTREAMNAME = "select s from LiveChannelStream s where s.dolbyStreamName=? and s.status<>-1";
    private static final String QUERY_CHANNELSTREAM_BY_CHANNELID_STREAMNAME = "select s from LiveChannelStream s where s.fatherId=? and s.streamName=? and s.status<>-1";
    private static final String QUERY_CHANNELSTREAM_BY_CHANNELID = "select s from LiveChannelStream s where s.fatherId=? and s.status<>-1 order by s.encodeType, s.streamType";
    private static final String QUERY_CHANNELSTREAM_BY_STREAMTYPE = "select s from LiveChannelStream s where s.fatherId=? and s.streamType=? and status<>-1";
    private static final String QUERY_CHANNELSTREAM_BY_STREAMTYPE_ISENABLE = "select s from LiveChannelStream s where s.fatherId=? and s.streamType=? and status<>-1 and isEnable=?";
    private static final String QUERY_CHANNELSTREAM_BY_STREAMTYPE_ENCODETYPE_ISENABLE = "select s from LiveChannelStream s where s.fatherId=? and s.streamType=? and s.encodeType=? and status<>-1 and isEnable=?";
    private static final String QUERY_CHANNELSTREAM_BY_STREAMTYPE_ENCODETYPE = "select s from LiveChannelStream s where s.fatherId=? and s.streamType=? and s.encodeType=? and status<>-1";
    private static final String QUERY_CHANNELSTREAM_BY_STREAMTYPE_NSTREAMNAME = "select s from LiveChannelStream s where s.fatherId=? and s.streamType=? and s.streamName<>? and status<>-1";
    private static final String QUERY_CHANNELSTREAM_BY_ISENABLE = "select s from LiveChannelStream s where s.fatherId=? and s.isEnable=? and status<>-1";
    private static final String QUERY_CHANNELSTREAM_BY_IP_NAME = "select s from LiveChannelStream s where s.streamIp=? and s.streamName=? and status<>-1";

    public void updateOnly(LiveChannelStream liveChannelStream) {
        try {
            if (null != liveChannelStream) {
                getSession().update(liveChannelStream);
                logger.debug("update entity: {}", liveChannelStream);
            }
        } catch (HibernateException e) {
            logger.info("[ChannelStreamBaseDao][updateOnly][LiveChannelStream={}][Exception={}]", liveChannelStream, e);
        }
    }

    public void delete(LiveChannelStream liveChannelStream) {
        if (liveChannelStream != null) {
            liveChannelStream.setStatus(-1);
            save(liveChannelStream);
        }
    }

    public void delete(Long fatherId, String streamName) {
        LiveChannelStream liveChannelStream = get(fatherId, streamName);
        if (liveChannelStream != null) {
            liveChannelStream.setStatus(-1);
            save(liveChannelStream);
        }
    }

    public LiveChannelStream getById(Long id) {
        return (LiveChannelStream) createQuery(QUERY_CHANNELSTREAM_BY_ID, id).uniqueResult();
    }

    public LiveChannelStream get(String streamName) {
        return (LiveChannelStream) createQuery(QUERY_CHANNELSTREAM_BY_STREAMNAME, streamName).uniqueResult();
    }

    public LiveChannelStream get(Long fatherId, String streamName) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(streamName);
        LiveChannelStream liveChannelStream = (LiveChannelStream) createQuery(QUERY_CHANNELSTREAM_BY_CHANNELID_STREAMNAME, list).uniqueResult();
        return liveChannelStream;
    }

    public LiveChannelStream get(String streamName, String streamIp) {
        List<Object> list = new ArrayList<Object>();
        list.add(streamName);
        list.add(streamIp);
        List<LiveChannelStream> liveChannelStreams = createQuery(QUERY_CHANNELSTREAM_BY_IP_NAME, list).list();
        if(liveChannelStreams.size()>0){
            return liveChannelStreams.get(0);
        }
        return null;
    }

    public List<LiveChannelStream> getByFatherId(Long fatherId) {
        return createQuery(QUERY_CHANNELSTREAM_BY_CHANNELID, fatherId).list();
    }

    public LiveChannelStream getByStreamType(Long fatherId, String streamType) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(streamType);
        LiveChannelStream liveChannelStream = (LiveChannelStream) createQuery(QUERY_CHANNELSTREAM_BY_STREAMTYPE, list).uniqueResult();
        return liveChannelStream;
    }

    public LiveChannelStream getByStreamType(Long fatherId, String streamType, String nStreamName) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(streamType);
        list.add(nStreamName);
        LiveChannelStream liveChannelStream = (LiveChannelStream) createQuery(QUERY_CHANNELSTREAM_BY_STREAMTYPE_NSTREAMNAME, list).uniqueResult();
        return liveChannelStream;
    }

    public LiveChannelStream getByStreamType(Long fatherId, String streamType, boolean isEnable) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(streamType);
        list.add(isEnable);
        LiveChannelStream liveChannelStream = (LiveChannelStream) createQuery(QUERY_CHANNELSTREAM_BY_STREAMTYPE_ISENABLE, list).uniqueResult();
        return liveChannelStream;
    }

    public List<LiveChannelStream> getByIsEnable(Long fatherId, boolean isEnable) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(isEnable);
        List<LiveChannelStream> liveChannelStreams = createQuery(QUERY_CHANNELSTREAM_BY_ISENABLE, list).list();
        return liveChannelStreams;
    }

//    public int count(Long fatherId) {
//        Object count = createQuery(QUERY_CHANNELSTREAM_COUNT_BY_CHANNELID, fatherId).uniqueResult();
//        String ret = count.toString();
//        return Integer.parseInt(ret);
//    }

    public long countLiveChannelStream(long fatherId) {
        Criteria criteria = createCriteria();
        criteria.add(Restrictions.eq("fatherId", fatherId));
        criteria.add(Restrictions.ne("status", LiveChannelStream.STATUS_DELETE));
        return countCriteriaResult(criteria);
    }

    public List<LiveChannelStream> getByStreamIp(String streamIp) {
        Criteria criteria = createCriteria();
        criteria.add(Restrictions.eq("streamIp", streamIp));
        criteria.add(Restrictions.ne("status", LiveChannelStream.STATUS_DELETE));
        return find(criteria);
    }

    public LiveChannelStream getByDolbyStreamName(String dolbyStreamName) {
        List<LiveChannelStream> liveChannelStreamList = createQuery(QUERY_CHANNELSTREAM_BY_DOLBYSTREAMNAME, dolbyStreamName).list();
        if (liveChannelStreamList.size() > 0) {
            return liveChannelStreamList.get(0);
        }
        return null;
    }

    public List<LiveChannelStream> getByStreamAddress(String streamAddress) {
        Criteria criteria = createCriteria();
        criteria.add(Restrictions.eq("streamAddress", streamAddress));
        criteria.add(Restrictions.ne("status", LiveChannelStream.STATUS_DELETE));
        return find(criteria);
    }

    public LiveChannelStream getByStreamTypeAndEncodeType(Long fatherId, String streamType, int encodeType, boolean isEnable) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(streamType);
        list.add(encodeType);
        list.add(isEnable);
        List<LiveChannelStream> liveChannelStreamList = createQuery(
                QUERY_CHANNELSTREAM_BY_STREAMTYPE_ENCODETYPE_ISENABLE, list).list();
        if (liveChannelStreamList.size() > 0) {
            return liveChannelStreamList.get(0);
        }
        return null;
    }

    public List<LiveChannelStream> getStreamsByStreamTypeAndEncodeType(Long fatherId, String streamType, int encodeType, boolean isEnable) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(streamType);
        list.add(encodeType);
//        list.add(isEnable);
        List<LiveChannelStream> liveChannelStreamList = createQuery(
                QUERY_CHANNELSTREAM_BY_STREAMTYPE_ENCODETYPE, list).list();
        return liveChannelStreamList;
    }
}